package com.service.cash.impl;

import com.bean.*;
import com.dao.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.cash.CashService;
import com.service.core.AbstractService;
import com.util.Config;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lvjianqing on 2017/9/16.
 */
@Service
public class CashServiceImpl extends AbstractService<Cash_apply, Integer> implements CashService {
    Logger log = Logger.getLogger(CashServiceImpl.class);
    @Autowired
    private Cash_applyMapper cash_applyMapper;
    @Autowired
    private Cash_payMapper cash_payMapper;
    @Autowired
    private Rebate_recordMapper rebate_recordMapper;
    @Autowired
    private Balance_recordMapper balance_recordMapper;
    @Autowired
    private Mb_accountMapper mb_accountMapper;

    @Override
    public Page findByPage(int pageNum, int pageSize, HashMap<String, Object> paraMap) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) cash_applyMapper.findByParaMap(paraMap);
        return page;
    }

    @Override
    public int saveCashRebate(Cash_apply cash, Mb_account account) throws Exception {
        log.info("\n佣金提现：" + cash.toString());
        BigDecimal amount = cash.getAmount();
        Mb_account mb = new Mb_account();
        mb.setMemberid(account.getMemberid());
        mb.setRebate(amount);
        //个人中户中的佣金减少
        mb_accountMapper.updateAccountSubtractByMemberid(mb);
        Float prop = StringUtil.String2Float(Config.getString("cash_discount", "0.03"));
        cash.setProp(prop);
        double z = (amount.doubleValue()) * (prop.floatValue());
        cash.setTax(BigDecimal.valueOf(z));
        cash.setPayamount(BigDecimal.valueOf(amount.doubleValue() - z));
        //提现申请记录表
        int i = cash_applyMapper.insertSelective(cash);
        if (i > 0) {
            Cash_pay pay = new Cash_pay();
            pay.setApplyid(cash.getId());
            pay.setPayamount(cash.getPayamount());
            pay.setPaytype(Integer.valueOf(cash.getPaytype()));
            pay.setPaytime(new Date());
            pay.setAliaccount(cash.getAliaccount());
            pay.setRealname(cash.getRealname());
            pay.setBankname(cash.getBankname());
            pay.setBankaccount(cash.getBankaccount());
            pay.setBankplace(cash.getBankplace());
            pay.setCategory(cash.getCategory());
            pay.setCityid(cash.getCityid());
            //打款记录表
            i = cash_payMapper.insertSelective(pay);
            if (i > 0) {
                Rebate_record rebate = new Rebate_record();
                rebate.setMemberid(cash.getMemberid());
                rebate.setAmount(cash.getAmount());
                rebate.setCreatetime(new Date());
                rebate.setType((byte) 2);
                rebate.setCityid(cash.getCityid());
                rebate.setCashapplyid(cash.getId());
                rebate.setIncome((byte) 0);
                i = rebate_recordMapper.insertSelective(rebate);
            }
        }
        return i;
    }

    @Override
    public int saveCashBalance(Cash_apply cash, Mb_account account) throws Exception {
        log.info("余额提现：" + cash.toString());
        BigDecimal amount = cash.getAmount();
        Mb_account mb = new Mb_account();
        mb.setMemberid(account.getMemberid());
        mb.setBalance(amount);
        mb_accountMapper.updateAccountSubtractByMemberid(mb);
        //提现记录表
        Float prop = StringUtil.String2Float(Config.getString("cash_discount", "0.03"));
        cash.setProp(prop);
        double z = (amount.doubleValue()) * (prop.floatValue());
        cash.setTax(BigDecimal.valueOf(z));
        cash.setPayamount(BigDecimal.valueOf(amount.doubleValue() - z));
        int i = cash_applyMapper.insertSelective(cash);
        if (i > 0) {
            Cash_pay pay = new Cash_pay();
            pay.setApplyid(cash.getId());
            pay.setPayamount(cash.getPayamount());
            pay.setPaytype(Integer.valueOf(cash.getPaytype()));
            pay.setPaytime(new Date());
            pay.setAliaccount(cash.getAliaccount());
            pay.setRealname(cash.getRealname());
            pay.setBankname(cash.getBankname());
            pay.setBankaccount(cash.getBankaccount());
            pay.setBankplace(cash.getBankplace());
            pay.setCategory(cash.getCategory());
            pay.setCityid(cash.getCityid());
            //打款记录表
            i = cash_payMapper.insertSelective(pay);
            if (i > 0) {
                Balance_record balance = new Balance_record();
                balance.setMemberid(cash.getMemberid());
                balance.setAmount(cash.getAmount());
                balance.setCreatetime(new Date());
                balance.setType((byte) 3);
                balance.setCityid(cash.getCityid());
                balance.setCashapplyid(cash.getId());
                balance.setIncome((byte) 0);
                i = balance_recordMapper.insertSelective(balance);
            }
        }
        return i;
    }

    @Override
    public Cash_pay findCashPayByApplyId(Integer applyId) {
        if (applyId == null) {
            return null;
        }
        return cash_payMapper.selectByApplyId(applyId);
    }

    @Override
    public int saveCashRebateVerify(Cash_apply cash, Cash_pay pay) {
        cash.setChecktime(new Date());
        cash_applyMapper.updateByPrimaryKeyWithBLOBs(cash);
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("cashapplyid", cash.getId());
        List<Rebate_record> list = rebate_recordMapper.findByParaMap(paraMap);
        if (list != null && list.size() > 0) {
            Rebate_record rebate = list.get(0);
            if (pay != null) {
                pay.setPaytime(new Date());
                cash_payMapper.updateByPrimaryKeySelective(pay);
                rebate.setType((byte) 3);
                rebate_recordMapper.updateByPrimaryKeySelective(rebate);
            } else {
                rebate.setType((byte) 4);
                rebate_recordMapper.updateByPrimaryKeySelective(rebate);
                Mb_account account = new Mb_account();
                account.setMemberid(cash.getMemberid());
                account.setRebate(cash.getAmount());
                mb_accountMapper.updateAccountPlusByMemberid(account);
            }
        }
        return 0;
    }

    @Override
    public int saveCashBalanceVerify(Cash_apply cash, Cash_pay pay) {
        cash.setChecktime(new Date());
        cash_applyMapper.updateByPrimaryKeyWithBLOBs(cash);
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("cashapplyid", cash.getId());
        List<Balance_record> list = balance_recordMapper.findByParaMap(paraMap);
        if (list != null && list.size() > 0) {
            Balance_record balance = list.get(0);
            if (pay != null) {
                pay.setPaytime(new Date());
                cash_payMapper.updateByPrimaryKeySelective(pay);
                balance.setType((byte) 4);
                balance_recordMapper.updateByPrimaryKeySelective(balance);
            } else {
                balance.setType((byte) 5);
                balance_recordMapper.updateByPrimaryKeySelective(balance);
                Mb_account account = new Mb_account();
                account.setMemberid(cash.getMemberid());
                account.setBalance(cash.getAmount());
                mb_accountMapper.updateAccountPlusByMemberid(account);
            }
        }
        return 0;
    }
}
